﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace SS.WPFusercontrol
{
    /// <summary>
    /// Interaction logic for Report_WPF.xaml
    /// </summary>
    public partial class Report_WPF : UserControl
    {
        public Report_WPF()
        {
            InitializeComponent();
            LoadChart();
        }

        private void LoadChart()
        {
            LineSeries lineSeries = new LineSeries();
            ColumnSeries columnSeries1 = new ColumnSeries();
            ColumnSeries columnSeries2 = new ColumnSeries();
            System.Windows.Data.Binding keyBinding = new System.Windows.Data.Binding();
            keyBinding.Path = new PropertyPath("Key");
            lineSeries.IndependentValueBinding = keyBinding;
            columnSeries1.IndependentValueBinding = keyBinding;
            columnSeries2.IndependentValueBinding = keyBinding;

            System.Windows.Data.Binding valueBinding = new System.Windows.Data.Binding();
            valueBinding.Path = new PropertyPath("Value");
            lineSeries.DependentValueBinding = valueBinding;
            columnSeries1.DependentValueBinding = valueBinding;
            columnSeries2.DependentValueBinding = valueBinding;

            //lineSeries.ItemsSource = new KeyValuePair<DateTime, int>[] {
            //       new KeyValuePair<DateTime, int>(DateTime.Now, 9), 
            //       new KeyValuePair<DateTime, int>(DateTime.Now.AddDays(1), 8), 
            //       new KeyValuePair<DateTime, int>(DateTime.Now.AddDays(3), 6), 
            //       new KeyValuePair<DateTime, int>(DateTime.Now.AddDays(2), 9), 
            //       new KeyValuePair<DateTime, int>(DateTime.Now.AddDays(4), 8) 
            //        };
            //columnSeries.ItemsSource = new KeyValuePair<DateTime, int>[] {
            //       new KeyValuePair<DateTime, int>(DateTime.Now, 9), 
            //       new KeyValuePair<DateTime, int>(DateTime.Now.AddDays(1), 8), 
            //       new KeyValuePair<DateTime, int>(DateTime.Now.AddDays(3), 6), 
            //       new KeyValuePair<DateTime, int>(DateTime.Now.AddDays(2), 9), 
            //       new KeyValuePair<DateTime, int>(DateTime.Now.AddDays(4), 8) 
            //        };
            //chart1.Series.Add(lineSeries);

            ProductChart.Title = "Columns Chart";
            columnSeries1.Title = "Hello World";
            columnSeries2.Title = "Hello World";
            CustomerChart.Title = "Line Chart";
            lineSeries.Title = "Hello World";


            string DBConnect = ConfigurationManager.ConnectionStrings["SS.Properties.Settings.SSZZConnectionString"].ToString();

            SqlConnection cn = new SqlConnection(DBConnect);

            SqlCommand cmd = cn.CreateCommand();

            //cmd.CommandText = "SELECT CategoryName, COUNT(*) as ProductCount FROM Products p INNER JOIN Categories c ON c.CategoryID = p.CategoryID GROUP BY CategoryName ORDER BY CategoryName";
            cmd.CommandText = "select P.ProductName AS ProductName, sum(S.SalesNumber * UnitPrice) AS SalesAmout from Products P, Sales S WHERE P.productid= S.productid  GROUP BY P.ProductName";
            cn.Open();

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);

            //SqlDataReader dr = cmd.ExecuteReader();
            //columnSeries.ItemsSource = dt.DefaultView;
            //chart1.DataBindTable(dr, "ProductName");
            //chart1.Series[0].Name="HS";
            columnSeries1.ItemsSource = tableSerialize(dt);
            columnSeries2.ItemsSource = tableSerialize(dt);
            lineSeries.ItemsSource = tableSerialize(dt);
            //dr.Close();


            ProductChart.Series.Add(columnSeries1);
            ProductChart.Series.Add(columnSeries2);
            ProductChart.Series.Add(lineSeries);
            //chart2.Series.Add(lineSeries);
            cn.Close();
        }

        private Dictionary<object, object> tableSerialize(DataTable dt)
        {
            Dictionary<object, object> result = new Dictionary<object, object>();
            foreach (DataRow  dr in dt.Rows)
            {
                result.Add(dr[0], dr[1]);
            }
            return result;
        }

        private void GenerateProductChartButton_Click(object sender, RoutedEventArgs e)
        {
            GenerateProductChart();
        }

        private void GenerateProductChart()
        {

        }
    }
}
